import Server from "@/config/server";
import request from '@/utils/request';
import { showErrors, showError, showSuccess } from '@/utils/showMsg';
import { promise } from "when";
import Vue from 'vue';
import { USER } from '@/store/mutation-types';
import env from '@/config/env';

const state = {
  UserDetail: '',
  gzWeChatQrCode: '',
};
const actions = {
  //用户信息
  userDetail({commit}, page) {
    return new promise(resData=>{
      request.get(Server.action.passport_detail, page )
      .then(response => {
        console.log(response);
        if (response.data.errorCode === 0) {
          commit("setUserDetail", response.data.result);
          Vue.ls.set(USER, response.data.result, env.tokenExpires);
        } else {
          showError( response.data.message )
        }
      })
      resData()
    })
  },
  // 获取绑定二维码
  getWeChatQrCode({commit}, id) {
    request.get(Server.action.wxGz_gzWeChatQrCode + '?user_id=' + id)
    .then(response => {
      console.log(response);
      if (response.data.errorCode === 0) {
        commit( 'setWeChatQrCode', response.data.result)
      } else {
        showError( '获取公众号二维码出错' )
      }
    })
  },
};
const mutations = {
  setUserDetail(state, text) {
	    state.UserDetail = text;
  },
  setWeChatQrCode(state, text) {
    state.gzWeChatQrCode = text;
},
};
export default {
    namespaced: true,
    state,
    actions,
    mutations
};
